﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_InsertGift : System.Web.UI.Page
{
    GiftBUS giftBUS = new GiftBUS();
    GCatalogBUS gCaBUS = new GCatalogBUS();
    DataTable tbCatalog = new DataTable();
    DataView viewCatalog;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
        }

    }
    private void LoadData()
    {
        tbCatalog = gCaBUS.returnAllGCatalog();
        viewCatalog = new DataView(tbCatalog);
        viewCatalog.RowFilter = "CG_Status=1";
        drlCatalog.DataSource = viewCatalog;
        drlCatalog.DataTextField = "CG_Name";
        drlCatalog.DataValueField = "CG_ID";
        drlCatalog.DataBind();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DataTable tbCheckID = new DataTable();
        float price = 0;
        string path = Server.MapPath("..");
        string id;
        string name;
        string details;
        string image;
        string imageCSDL;
        int cid;
        if (txtID.Value.Equals("") || txtName.Value.Equals("") || txtPrice.Value.Equals("") || txtDetails.Text.Equals(""))
        {
            lblError.Text = "- You must enter enough information";
        }
        else
        {
            try
            {
                price = float.Parse(txtPrice.Value);
                if (FileUpload1.HasFile)
                {
                    try
                    {
                        image = path + "\\images\\" + FileUpload1.FileName;
                        imageCSDL = "../images/" + FileUpload1.FileName;
                        FileUpload1.SaveAs(image);
                        id = "GI" + txtID.Value;
                        name = txtName.Value;
                        price = float.Parse(txtPrice.Value);
                        details = txtDetails.Text;
                        cid = Int32.Parse(drlCatalog.SelectedValue.ToString());
                        tbCheckID = giftBUS.returnGiftByID(id);

                        if (tbCheckID.Rows.Count > 0)
                        {
                            lblError.Text = "Id Gift exists";

                        }
                        else
                        {
                            bool check = giftBUS.insertGift(id, name, price, details, imageCSDL, cid);
                            if (check)
                            {
                                lblError.Text = "- Insert Gift Success";
                                txtID.Value = "";
                                txtName.Value = "";
                                txtPrice.Value = "";
                                txtDetails.Text = "";
                            }
                            else
                            {
                                lblError.Text = "- Insert Gift Error";
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        lblError.Text = "- Upload file error";
                    }

                }
                else
                {
                    lblError.Text = "- You must choose a file picture";
                }
            }
            catch (Exception ex)
            {
                lblError.Text = "- Price must be numeric";
                txtPrice.Value = "";
            }

        }

    }
}

